package com.icangya.DAO.impl;

import com.icangya.DAO.IBannersDAO;
import com.icangya.domain.Banners;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Repository
public class BannersDAOImpl extends BaseDAO<Banners> implements IBannersDAO{

    @Override
    public List<Banners> getAllBanners() {
        String hql = " FROM Banners b order by b.id desc ";
        List<Banners> res= new ArrayList<>();
        try{
            res= (List<Banners>) getHibernateTemplate().find(hql);

        }catch(DataAccessException e){
            e.printStackTrace();
            return res;
        }
        return res;
    }


    @Override
    public Serializable SaveNewOne(String path) {
        Banners banner = new Banners(path);
        Serializable id=getHibernateTemplate().save(banner);
        getHibernateTemplate().flush();
        return id;

    }

    @Override
    public boolean deleteBanner(Banners bn) {
        try{
            getHibernateTemplate().delete(bn);
        } catch (DataAccessException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Banners getByid(long id) {
        String hql=" FROM Banners bns where bns.id=?";
        Banners result;
        try{
            result = (Banners) getHibernateTemplate().find(hql, id).get(0);
        }catch (IndexOutOfBoundsException e){
            e.printStackTrace();
            return null;
        }
        return result;
    }

}
